import {observable, action, computed, toJS, runInAction} from 'mobx';
import {Tools, Toast} from 'weaver-mobile';
import * as API from '../../apis/signlog/UpdateSignApi'
import {Form, ListAdvanced} from 'weaver-mobile-page';
import objectAssign from "object-assign";

const getLabel = Tools.getLabel;
export default class UpdateSignLogStore {
    @observable loading = false;
    @observable form = new Form.Store({route: '/form/demo-0'});
    @observable signinput = {};
    @observable datas = [];

    @observable listAd = new ListAdvanced.Store();
    @observable isShowSignInput = false;
    @observable sumitParams = {};
    @observable coreParams = {};
    savenumber = 0;

    init = (prepath = "/odocComs", datas = [], commonParam) => {
        runInAction(() => {
            this.datas = datas;
            this.listAd = new ListAdvanced.Store(
                {
                    route: prepath, id: "update"
                }
            );
            this.coreParams = objectAssign({isMobileForm: 1}, commonParam);

            this.reload();
            /*let params = {params: {...commonParam}}
            this.listAd && this.listAd.getData({
                url:"/api/odoc/signInputUpdate/getSignLogSessionkey",
                method:"POST",
                ...params
            })*/
        })
    }

    reload = () => {
        const {searchAdvancedStore, listViewStore, searchParams } = this.listAd;
        let sparams = { ...searchParams };
        // 获取高级搜索参数
        if (searchAdvancedStore) {
            const { formParams } = searchAdvancedStore.form;
            sparams = { ...formParams };
        }
        listViewStore.selectNone();
        listViewStore.clearData();
        let params = {params: {...this.coreParams, ...sparams}}
        this.listAd && this.listAd.getData({
            url: "/api/odoc/signInputUpdate/getSignLogSessionkey",
            method: "POST",
            ...params
        })
    }

    /**
     * 保存数据
     * @param workflowStores
     * @param onCancel
     */
    doSave = (params = {}, onCancel) => {
        let param = objectAssign({}, this.coreParams, this.sumitParams, params);
        this.loading = false;
        API.doUpdateSign(param).then(res => {
            const {api_status = false, api_message = ""} = res;
            this.loading = true;
            this.isShowSignInput = false;

            this.reload();
            if (api_status) {
                this.savenumber++;
                Toast.success(getLabel(83551, "保存成功！"), 3, () => onCancel(res))
            } else {
                Toast.fail(api_message || getLabel(387623, "修改失败！"));
            }
        })
    }

    doDeleteSignLogByLogId = (data = {}) => {
        let params = objectAssign({}, this.coreParams, this.sumitParams, data);
        //删除成功刷新数据，删除失败提示失败
        API.doDeleteSign(params).then(res => {
            const {api_status = false, api_message} = res;
            this.reload();
            if (api_status) {//删除成功准备刷新数据
                this.savenumber++;
                Toast.success(api_message || getLabel(83472, "删除成功！"))
            } else {//删除失败提示信息
                Toast.fail(api_message || getLabel(83473, "删除失败！"))
            }
        })
    }

    appendPickerRemark = (v = "") => {
        const {remark = ""} = this.sumitParams;
        let newremark = `${remark}${v}`;
        this.changeSubmitParams({remark: newremark})

    }

    @action
    getSignInput = (para = {logid: ""}) => {
        this.loading = false;
        this.isShowSignInput = true;

        let params = objectAssign({isOdoc:true, getOdocSignInput:true}, this.coreParams, this.sumitParams, para);
        API.getSignInput(params).then(res => {
            const {remarkInfo = {}, logid, nodeid} = res;
            const {signHandWritten = {}} = remarkInfo;
            const {fileid} = signHandWritten;
            runInAction(() => {
                this.loading = true;
                this.changeSubmitParams({...para, ...remarkInfo, handWrittenSign: fileid});
                this.signinput = res;
            })
        })
    }

    @action
    hiddenSignInput = () => {
        this.isShowSignInput = false;
        this.loading = false;
        this.sumitParams = {};
		const viewport = document.querySelector("meta[name=viewport]");
		if(viewport != null){
			/*if(isReadPCLayout && pathname === "/req" && !showSignArea){
				const screenWidth = window.screen.width;
				const contentWidth = jQuery(".excelMainTable") ? jQuery(".excelMainTable").width() : document.body.scrollWidth;
				const defScale = screenWidth/(contentWidth+50);
				viewport.setAttribute("content", "width=device-width, initial-scale="+defScale+", maximum-scale=5.0, minimum-scale=0.1");
			}else*/
			viewport.setAttribute("content", "width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0");
		}
	}

    @action
    changeSubmitParams = (datas) => {
        this.sumitParams = objectAssign({}, this.sumitParams, datas);
    }

    destory = () => {
        this.signinput = {};
        this.isShowSignInput = false;
        this.sumitParams = {};
        this.savenumber = 0;
    }
}
